package md.mru.frontend.controllers;

import md.mru.core.dtos.*;
import md.mru.core.service.AdditionalDataService;
import org.primefaces.context.RequestContext;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 29.03.2014
 */
@ManagedBean(name = "additionalDataBean")
@ViewScoped
public class AdditionalDataAdministrationBean extends BackingBean {

    private static final long serialVersionUID = -3743180383267802165L;
    @ManagedProperty(value = "#{additionalDataService}")
    private AdditionalDataService additionalDataService;
    @ManagedProperty(value = "#{folderAdministrationBean}")
    private FolderAdministrationBean folderAdministrationBean;

    private List<CitizenDto> citizens = new ArrayList<>();
    private List<CitizenshipDto> citizenShips = new ArrayList<>();
    private List<DocumentDto> documents = new ArrayList<>();
    private List<DocumentTypeDto> documentTypes = new ArrayList<>();
    private List<FamilyMemberDto> familyMembers = new ArrayList<>();
    private List<FamilyRelationDto> familyRelations = new ArrayList<>();
    private CitizenDto selectedCitizen;
    private DocumentDto selectedDocument;
    private FamilyMemberDto selectedFamilyMember;

    public void initAdditionalDataTab() {
        citizens = this.additionalDataService.getCitizens(this.folderAdministrationBean.getSelectedFolder().getId());
        documents = this.additionalDataService.getDocuments(this.folderAdministrationBean.getSelectedFolder().getId());
        familyMembers = this.additionalDataService.getFamilyMembers(this.folderAdministrationBean.getSelectedFolder().getId());
    }

    public void resetCreateFamilyDialog() {
        this.selectedFamilyMember = new FamilyMemberDto();
        this.selectedFamilyMember.setFolder(this.folderAdministrationBean.getSelectedFolder().getId());
    }

    public void initEditFamilyMemberDialog(FamilyMemberDto familyMember) {
        this.selectedFamilyMember = familyMember;
        this.initFamilyMemberDialog();
    }

    private void initFamilyMemberDialog() {
        if (this.familyRelations.isEmpty()) {
            familyRelations = this.additionalDataService.getFamilyRelations();
            this.getObjectMappingConverter().mapObjectList(familyRelations);
        }
    }

    public void saveFamilyMembers() {
        long id = additionalDataService.saveFamilyMember(selectedFamilyMember);
        if (selectedFamilyMember.getId() == 0) {
            selectedFamilyMember.setId(id);
            familyMembers.add(selectedFamilyMember);
        }
    }

    public void resetCreateDocumentDialog() {
        this.selectedDocument = new DocumentDto();
        this.selectedDocument.setFolder(this.folderAdministrationBean.getSelectedFolder().getId());
        this.initDocumentDialog();
    }

    private void initDocumentDialog() {
        if (this.documentTypes.isEmpty()) {
            documentTypes = this.additionalDataService.getDocumentTypes();
            this.getObjectMappingConverter().mapObjectList(documentTypes);
        }
    }

    public void saveDocument() {
        long id = additionalDataService.saveDocument(selectedDocument);
        if (selectedDocument.getId() == 0) {
            selectedDocument.setId(id);
            documents.add(selectedDocument);
        }
    }

    public void initEditDocumentDialog(DocumentDto document) {
        this.selectedDocument = document;
        this.initDocumentDialog();
    }

    public void resetCreateCitizenDialog() {
        this.selectedCitizen = new CitizenDto();
        this.selectedCitizen.setFolder(this.folderAdministrationBean.getSelectedFolder().getId());
        this.initCitizenDialog();
    }

    private void initCitizenDialog() {
        if (this.citizenShips.isEmpty()) {
            citizenShips = this.additionalDataService.getCitizenShips();
            this.getObjectMappingConverter().mapObjectList(citizenShips);
        }
    }

    public void initEditCitizenDialog(CitizenDto citizen) {
        this.selectedCitizen = citizen;
        this.initCitizenDialog();
    }


    public void saveCitizen() {
        long id = additionalDataService.saveCitizen(selectedCitizen);
        if (selectedCitizen.getId() == 0) {
            selectedCitizen.setId(id);
            citizens.add(selectedCitizen);
        }
    }

    public void deleteCitizen(CitizenDto c) {
        this.additionalDataService.deleteCitizen(c.getId());
        this.citizens.remove(c);
    }

    public void addNewCitizen(String editRowId) {
        CitizenDto c = new CitizenDto();
        citizens.add(c);
        RequestContext.getCurrentInstance().execute(super.constructOnCompleteScript("cz-formId", "czData", editRowId));
    }

    public void deleteDocument(DocumentDto d) {
        additionalDataService.deleteDocument(d.getId());
        this.documents.remove(d);
    }

    public void addNewDocument(String editRowId) {
        DocumentDto d = new DocumentDto();
        documents.add(d);
        RequestContext.getCurrentInstance().execute(super.constructOnCompleteScript("doc-formId", "docData", editRowId));
    }

    public void deleteFamilyMember(FamilyMemberDto fm) {
        additionalDataService.deleteFamilyMember(fm.getId());
        this.familyMembers.remove(fm);
    }

    public void addNewFamilyMember(String editRowId) {
        FamilyMemberDto fm = new FamilyMemberDto();
        familyMembers.add(fm);
        RequestContext.getCurrentInstance().execute(super.constructOnCompleteScript("fm-formId", "fmData", editRowId));
    }

    public List<CitizenDto> getCitizens() {
        if (citizens == null) {
            citizens = new ArrayList<>(0);
        }
        return citizens;
    }

    public void setCitizens(List<CitizenDto> citizens) {
        this.citizens = citizens;
    }

    public List<DocumentDto> getDocuments() {
        return documents;
    }

    public void setDocuments(List<DocumentDto> documents) {
        this.documents = documents;
    }

    public List<DocumentTypeDto> getDocumentTypes() {
        return documentTypes;
    }

    public void setDocumentTypes(List<DocumentTypeDto> documentTypes) {
        this.documentTypes = documentTypes;
    }

    public List<CitizenshipDto> getCitizenShips() {
        return citizenShips;
    }

    public void setCitizenShips(List<CitizenshipDto> citizenShips) {
        this.citizenShips = citizenShips;
    }

    public List<FamilyMemberDto> getFamilyMembers() {
        return familyMembers;
    }

    public void setFamilyMembers(List<FamilyMemberDto> familyMembers) {
        this.familyMembers = familyMembers;
    }

    public List<FamilyRelationDto> getFamilyRelations() {
        return familyRelations;
    }

    public void setFamilyRelations(List<FamilyRelationDto> familyRelations) {
        this.familyRelations = familyRelations;
    }

    public CitizenDto getSelectedCitizen() {
        return selectedCitizen;
    }

    public void setSelectedCitizen(CitizenDto selectedCitizen) {
        this.selectedCitizen = selectedCitizen;
    }

    public void setAdditionalDataService(AdditionalDataService additionalDataService) {
        this.additionalDataService = additionalDataService;
    }

    public void setFolderAdministrationBean(FolderAdministrationBean folderAdministrationBean) {
        this.folderAdministrationBean = folderAdministrationBean;
    }

    public DocumentDto getSelectedDocument() {
        return selectedDocument;
    }

    public void setSelectedDocument(DocumentDto selectedDocument) {
        this.selectedDocument = selectedDocument;
    }

    public FamilyMemberDto getSelectedFamilyMember() {
        return selectedFamilyMember;
    }

    public void setSelectedFamilyMember(FamilyMemberDto selectedFamilyMember) {
        this.selectedFamilyMember = selectedFamilyMember;
    }
}
